package fuza.android.thetrafficsigns;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;

import fuza.android.thetrafficsigns.help.ViewPagerAdapter;

public class MainActivity extends SherlockFragmentActivity {

	ActionBar mActbar;
	ViewPager mPager;
	Tab tab;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Activate Navigation Mode Tabs
		mActbar = getSupportActionBar();
		mActbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Locate ViewPager in activity_main.xml
		mPager = (ViewPager) findViewById(R.id.pager);

		// Activate Fragment Manager
		FragmentManager fm = getSupportFragmentManager();

		// Capture ViewPager page swipes
		ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				// Find the ViewPager Position
				mActbar.setSelectedNavigationItem(position);
			}
		};

		mPager.setOnPageChangeListener(ViewPagerListener);
		ViewPagerAdapter vipageadapter = new ViewPagerAdapter(fm);
		mPager.setAdapter(vipageadapter);

		// Capture tab button clicks
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {

			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				// TODO Auto-generated method stub
				mPager.setCurrentItem(tab.getPosition());
			}

			@Override
			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTabReselected(Tab tab, FragmentTransaction ft) {
				// TODO Auto-generated method stub

			}
		};

		// membuat tab pertama
		tab = mActbar.newTab().setText("TRAFFIC SIGNS")
				.setTabListener(tabListener);
		mActbar.addTab(tab);

		// membuat tab kedua
		tab = mActbar.newTab().setText("TRAFFIC LAWS")
				.setTabListener(tabListener);
		mActbar.addTab(tab);

		// membuat tab ketiga
//		tab = mActbar.newTab().setText("HELP").setTabListener(tabListener);
//		mActbar.addTab(tab);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SubMenu subMenu1 = menu.addSubMenu(0, Menu.FIRST, Menu.NONE, "");
		subMenu1.add(0, 11, Menu.NONE, "About").setIcon(R.drawable.ic_menu_2);
		subMenu1.add(0, 12, Menu.NONE, "Search").setIcon(R.drawable.i_search);
		subMenu1.add(0, 13, Menu.NONE, "Help").setIcon(R.drawable.i_help);

		MenuItem SubMenu1Item = subMenu1.getItem();
		SubMenu1Item.setIcon(R.drawable.ic_action_overflow);
		SubMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
				| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 11:
			tampilkanAbout();
			return true;
		case 12:
			Intent isearch = new Intent(this, SearchActivity.class);
			startActivity(isearch);
			
			return true;
		case 13:
			Intent ihelp = new Intent(this, FragmentHelp.class);
			startActivity(ihelp);
			
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void tampilkanAbout() {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View aboutApp = inflater.inflate(R.layout.scroll_text, null, false);

		TextView tv = (TextView) aboutApp.findViewById(R.id.text_head);
		tv.setText("About This Apps.");
		new AlertDialog.Builder(MainActivity.this).setCustomTitle(aboutApp).setMessage("This application is made with the purpose of, providing information on traffic signs in Indonesia, along with sanctions if violated, thus more road users are expected to obey the rules.")
				.setTitle("About")
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

					@TargetApi(17)
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).show();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
			final View header = inflater.inflate(R.layout.dialog_customtitle, null);
			TextView tx = (TextView) header.findViewById(R.id.text_title);
			tx.setText("Exit ?");
			new AlertDialog.Builder(MainActivity.this).setCustomTitle(header).setMessage("Apakah anda yakin akan keluar?")
			.setPositiveButton("Ya",
					new DialogInterface.OnClickListener() {
						@TargetApi(11)
						public void onClick(DialogInterface dialog, int id) {
							finish();
						}
					})
			.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
				@TargetApi(11)
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			}).show();
		}
		return super.onKeyDown(keyCode, event);
	}

}
